﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.Models;

namespace WebUI.UserControls
{
    public partial class UCPager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Khai bao 1 parameter la Func de su dung ham GetLink trong helper,
        /// hoac don gian su dung Helper.link trong nay luon.
        /// </summary>
        /// <param name="pagingInfo"></param>
        /// <param name="getLink"></param>
        public void DisplayPager(Models.PagingInfo pagingInfo, Func<string,int,string> getLink)
        {
            if(pagingInfo.HowManyPage!=1)
            {
                if (pagingInfo.Pagesize > 0)
                {
                    this.Visible = true;
                }

                if (pagingInfo.CurrentPage == 1)
                {
                    hplBack.Visible = false;
                }
                else
                {
                    hplBack.NavigateUrl = (pagingInfo.CurrentPage == 2)
                                              ? getLink(pagingInfo.PageUrlLink, 1)
                                              : getLink(pagingInfo.PageUrlLink, pagingInfo.CurrentPage - 1);
                }

                if (pagingInfo.CurrentPage == pagingInfo.HowManyPage)
                {
                    hplPrev.Visible = false;
                }
                else
                {
                    hplPrev.NavigateUrl = getLink(pagingInfo.PageUrlLink, pagingInfo.CurrentPage + 1);
                }

                var pagers = new PagerLink[pagingInfo.HowManyPage];

                for (int i = 0; i < pagingInfo.HowManyPage; i++)
                {
                    pagers[i] = new PagerLink
                    {
                        Page = (i + 1).ToString(),
                        Url = getLink(pagingInfo.PageUrlLink, i + 1)
                    };
                }

                rptPager.DataSource = pagers;
                rptPager.DataBind();
            }
            else
            {
                this.Visible = false;
            }
            
        }
    }
}